import React, {Component} from 'react';
import Child from "./Child"

class Parent extends Component {

    state = {
        hasError: ''

    }

    // 当子组件发生错误时，会触发此函数，并携带错误信息
    static getDerivedStateFromError(error) {
        console.log('error', error)
        return {
            hasError: error
        }
    }

    componentDidCatch(error, errorInfo) {
        // 统计页面的错误。发送请求发送到后台去
        console.log('@@@',error)
    }

    render() {
        return (
            <div>
                <h2>我是parent组件</h2>
                {
                    this.state.hasError ? <h2>当前网络错误,稍后再试</h2> : <Child/>
                }


            </div>
        );
    }
}

export default Parent;
